var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
var session = require('express-session');

var main_frame_router = require('./routes/main_frame');
var create_wallet_router = require('./routes/create_wallet');
var recharge_router = require('./routes/recharge')
var login_router = require('./routes/login');
var logout_router = require('./routes/logout')
var register_router = require('./routes/register');
var services_router = require('./routes/services');
var cr_upload_router = require('./routes/cr_upload');
var cr_transfer_router = require('./routes/cr_transfer');
var cr_pledge_router = require('./routes/cr_pledge');
var cr_unpledge_router = require('./routes/cr_unpledge');
var cr_authorize_router = require('./routes/cr_authorize');
var cr_unauthorize_router = require('./routes/cr_unauthorize');
var recharge_router = require('./routes/recharge');
var personal_page_router = require('./routes/personal_page');
var check_state_router = require('./routes/check_state');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
  secret: 'secret',
  resave: false,
  saveUninitialized: true
}));

app.use('/', main_frame_router);
app.get('/login', login_router.form);
app.get('/logout', logout_router.form);
app.get('/create_wallet', create_wallet_router.create_wallet);
app.get('/recharge', recharge_router.form);
app.get('/register', register_router.form);
app.get('/services', services_router.form);
app.get('/cr_upload', cr_upload_router.form);
app.get('/cr_transfer', cr_transfer_router.form);
app.get('/cr_pledge', cr_pledge_router.form);
app.get('/cr_unpledge', cr_unpledge_router.form);
app.get('/cr_authorize', cr_authorize_router.form);
app.get('/cr_unauthorize', cr_unauthorize_router.form);
app.get('/recharge', recharge_router.form);
app.get('/personal_page', personal_page_router.form);
app.get('/balance', personal_page_router.balance);
app.get('/copyright', personal_page_router.copyright);
app.get('/history', personal_page_router.history);
app.get('/check_state', check_state_router.form);
app.get('/check_state_submit', check_state_router.check_state);

app.post('/login', login_router.submit);
app.post('/logout', logout_router.submit);
app.post('/recharge', recharge_router.recharge);
app.post('/register', register_router.submit);
app.post('/cr_upload', cr_upload_router.upload);
app.post('/cr_transfer', cr_transfer_router.transfer);
app.post('/cr_plegde', cr_pledge_router.pledge);
app.post('/cr_authorize', cr_authorize_router.authorize);
app.post('/cr_unpledge', cr_unpledge_router.unpledge);
app.post('/cr_unauthorize', cr_unauthorize_router.unauthorize);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
